package com.webDramaUSA.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.webDramaUSA.model.User;
import com.webDramaUSA.service.IUserService;
import freemarker.template.TemplateException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * Created by Aaron on 2017/10/28.
 */

/**
 * 此为搭建环境使用的测试页面
 */
@Controller
@RequestMapping("/user")
public class UserController {

    @Resource
    private IUserService userService;

    @RequestMapping("/showUser.do")
    public void selectUser(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        long userId = Long.parseLong(request.getParameter("id"));
        User user = this.userService.selectUser(userId);
        ObjectMapper mapper = new ObjectMapper();
        response.getWriter().write(mapper.writeValueAsString(user));
        response.getWriter().close();
    }
    @RequestMapping(value = "/testFreeMakeer.do",method = RequestMethod.GET)
    public ModelAndView testFreeMakeer(HttpServletRequest request, HttpServletResponse response) throws IOException, TemplateException {
        ModelAndView mav=new ModelAndView("test");
        String str = "测试freemakeer";
        mav.addObject("test",str);
        List<User> list = this.userService.selectAllUser();
        mav.addObject("list",list);
        return mav;
    }

    @RequestMapping(value = "/idx.do",method = RequestMethod.GET)
    public ModelAndView testindex(HttpServletRequest request, HttpServletResponse response) throws IOException, TemplateException {
        ModelAndView mav=new ModelAndView("idx");
        return mav;
    }

}